<?php

namespace App\Http\Controllers\Api;

use App\Analysitical;
use Illuminate\Http\Request;

class AnalysesController extends BaseController
{
    /**
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $orderBy = $request->order_by ? $request->order_by : 'id';
        $orderType = $request->order_type ? $request->order_type : 'desc';
        $pageSize = $request->page_size ? $request->page_size : $this->pageSize;
        $lists = Analysitical::orderBy($orderBy, $orderType)->with(['sort']);
        if ($request->get('subject_sort_id')) {
            $lists = $lists->where('subject_sort_id', $request->subject_sort_id);
        }
        if ($request->get('answer')) {
            $lists = $lists->where('answer', $request->answer);
        }
        if ($request->get('title')) {
            $lists = $lists->where('title', $request->title);
        }
        return $lists->paginate($pageSize);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        Analysitical::create($request->all());
        return response(['message' => '创建成功']);
    }
    /**
     * Display the specified resource.
     *
     * @param  \App\Analysitical  $analysitical
     * @return \Illuminate\Http\Response
     */
    public function show(Analysitical $analysitical)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Analysitical  $analysitical
     * @return \Illuminate\Http\Response
     */
    public function edit(Analysitical $analysitical)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Analysitical  $analysitical
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Analysitical $analysitical)
    {
        $res=$analysitical->update($request->only(['title', 'answer', 'analysis']));
        if(!$res){
            return response(['message' => '修改失败'],400);
        }
        return response(['message' => '修改成功']);
    }

    /**
     * @param Analysitical $analysitical
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function destroy(Analysitical $analysitical)
    {
        $analysitical->delete();
        return response(['message' => '删除成功']);
    }
}
